﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ECS.DAL.Entity;
using ECS.DAL.Component;
using ECS.Framework;

namespace ECS.BLL.Component
{
    public class AuthorizationManager
    {
        #region private members
        /// <summary>
        /// list of authorization
        /// </summary>
        private List<UserAuthorization> _authList = new List<UserAuthorization>();
        #endregion

        #region public members
        
        #endregion

        #region private methods
        #endregion

        #region public methods
        /// <summary>
        /// get authorization menu list
        /// </summary>
        /// <param name="userId"></param>
        public void GetUserAuthorization(string userId, string appId, string device, string platform, int locale)
        {
            UserAuthorizationComponent compData = null;
            UserAuthorization param = null;
            List<ObjectContext> temp = new List<ObjectContext>();
            param = new UserAuthorization();
            param.UserId = userId;
            param.AppId = appId;
            param.Device = device;
            param.Platform = platform;
            param.Locale = locale;
            compData = new UserAuthorizationComponent();
            //temp = compData.Search(param);
            temp = compData.GetUserMenu(param);
            foreach (ObjectContext item in temp)
            {
                _authList.Add((UserAuthorization)item);
            }
        }
        /// <summary>
        /// Check authorization on menu
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public bool IsValidAuthorization(string menuId)
        {
            bool isAuthor = false; //default is FALSE
            var result = _authList.Where(o => o.MenuName.Equals(menuId));
            if (result.Count() > 0)
            {
                foreach (UserAuthorization ua in result)
                {
                    isAuthor = ua.IsActive;
                    break;
                }
            }
            return isAuthor;
        }
        #endregion
    }
}
